package com.toocms.jinjuhang.ui.main

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.*
import com.qmuiteam.qmui.widget.dialog.QMUIDialog.CheckBoxMessageDialogBuilder
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.databinding.FgtMainBinding
import com.toocms.jinjuhang.dialog.hint.HintDialog
import com.toocms.jinjuhang.ui.base.BaseFgt
import com.toocms.jinjuhang.ui.cart.CartFgt
import com.toocms.jinjuhang.ui.classify.ClassifyFgt
import com.toocms.jinjuhang.ui.index.IndexFgt
import com.toocms.jinjuhang.ui.live_streaming.LiveStreamingFgt
import com.toocms.jinjuhang.ui.mine.MineFgt
import com.toocms.jinjuhang.ui.mine.setting.agreement.AgreementFgt
import com.toocms.tab.TooCMSApplication
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.expand.agreement.AgreementDialog
import com.toocms.tab.widget.update.UpdateManager

class MainFgt : BaseFgt<FgtMainBinding, MainModel>() {

    override fun onFragmentCreated() {
        topBar.visibility = View.GONE
        if (checkConsentAgreement()) {
            UpdateManager.checkUpdate()
        }
        // 是否跳转直播页
        if (arguments != null && arguments?.getBoolean(
                "isLive",
                false
            )!!
        ) {
            viewModel.liveStreamingClickBindingCommand.execute()
        }
    }

    override fun getLayoutResId() = R.layout.fgt_main

    override fun getVariableId() = BR.mainModel

    override fun viewObserver() {
        viewModel.contentSingleLiveEvent.observe(this) {
            var fgt: Fragment? = null
            when (it) {
                MainFilter.INDEX -> {
                    fgt = IndexFgt()
                }

                MainFilter.CLASSIFY -> {
                    fgt = ClassifyFgt()
                }

                MainFilter.LIVE_STREAMING -> {
                    fgt = LiveStreamingFgt()
                }

                MainFilter.CART -> {
                    fgt = CartFgt()
                }

                MainFilter.MINE -> {
                    fgt = MineFgt()
                }
            }
            fgt?.apply {
                val transaction = this@MainFgt.childFragmentManager.beginTransaction()
                if (transaction.isEmpty) {
                    transaction.add(R.id.content_fl, this, this.javaClass.simpleName)
                } else {
                    transaction.replace(R.id.content_fl, this, this.javaClass.simpleName)
                }
                transaction.commit()
            }
        }

        viewModel.showDialogEvent.observe(this, Observer {
            val builder = CheckBoxMessageDialogBuilder(context)
                .setTitle("是否确定已知晓本次活动内容？")
                .setMessage("不再提示")
            builder.addAction("我已知晓") { dialog, _ ->
                dialog.dismiss()
                viewModel.isShowDialog.set(false)
                if (builder.isChecked) viewModel.close_alert()
            }
            builder.show()
        })
    }


    private fun checkConsentAgreement(): Boolean {
        if (!TooCMSApplication.getInstance().isConsentAgreement) {
            AgreementDialog()
                .setPolicyClickListener { view: View? ->
                    val params = Bundle()
                    params.putString(
                        Constants.BundleKey.KEY_TYPE,
                        Constants.Extra.AGREEMENT_TYPE_PRIVACY
                    )
                    startFragment(AgreementFgt::class.java, params)
                }
                .setAgreementClickListener { view: View? ->
                    val params = Bundle()
                    params.putString(
                        Constants.BundleKey.KEY_TYPE,
                        Constants.Extra.AGREEMENT_TYPE_USER
                    )
                    startFragment(AgreementFgt::class.java, params)
                }
                .setAgreeClickListener { view: View? ->
                    UpdateManager.checkUpdate()
                }
                .show(childFragmentManager, null)
        }
        return TooCMSApplication.getInstance().isConsentAgreement
    }

    override fun onBackPressed() {
        if (MainFilter.INDEX != viewModel.currentFilterObservableField.get()) {
            viewModel.indexClickBindingCommand.execute()
            return
        }
        HintDialog().apply {
            title = StringUtils.getString(R.string.str_hint)
            content = StringUtils.getString(R.string.str_exit_app_hint)
            onPositiveClickListener = {
                super.onBackPressed()
            }
            passive = StringUtils.getString(R.string.str_cancel)
            positive = StringUtils.getString(R.string.str_confirm)
            show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null)
        }
    }

}